
<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import Bubble from '../../components/bubble.vue'
import request from '@/request'
import { useUserInfo } from '../../store/user'
import { useMenu } from '../../store/menu'
const menu = useMenu()
const router = useRouter()
const formData = reactive({
  account: 'superadmin',
  password: '123456'
})

const rules = {
  account: [{ required: true, message: '姓名必填', type: 'error' }],
  password: [{ required: true, message: '密码必填', type: 'error' }]
}

const user = useUserInfo()
const loginForm = ref(null)
const submitForm = async () => {
  if (!loginForm.value) return
  await loginForm.value.validate((valid, fields) => {
    if (valid) {
      request({
        url: '/login',
        method: 'post',
        data: formData
      }).then(res => {
        ElMessage.success('登录成功')
        user.setUserInfo(res.userInfo)
        sessionStorage.setItem('tokenId', 'abcdefg')
        router.push(menu.menu[0].path)
      }).catch(err => {
        console.log(err)
        ElMessage.error(err)
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

//  气泡
const bubbleList = [
  {
    size: '100px',
    delay: '2s',
    style: {
      left: '20px',
      animationDuration: '4s'
    },
    sway: 80
  },
  {
    size: '60px',
    delay: '3s',
    style: {
      left: '100px',
      animationDuration: '8s'
    }
  },
  {
    size: '80px',
    delay: '4s',
    style: {
      left: '200px',
      animationDuration: '6s'
    },
    sway: 160
  },
  {
    size: '120px',
    delay: '5s',
    style: {
      left: '450px'
    }
  },
  {
    size: '140px',
    delay: '6s',
    style: {
      left: '700px'
    },
    sway: 300
  },
  {
    size: '160px',
    delay: '7s',
    style: {
      left: '1000px'
    }
  },
  {
    size: '160px',
    delay: '3s',
    style: {
      left: '1300px'
    },
    sway: 100
  },
  {
    size: '100px',
    delay: '1s',
    style: {
      left: '1200px'
    }
  },
  {
    size: '160px',
    delay: '5s',
    style: {
      left: '100px'
    }
  }
]
</script>

<template>
  <div class="login-body">
    <el-form class="login-form" ref="loginForm" :rules="rules" :model="formData" :colon="true" :label-width="0">
      <el-form-item>
        <div class="admin-name">Vue Element Plus Admin</div>
      </el-form-item>
      <el-form-item name="account">
        <el-input v-model="formData.account" clearable placeholder="请输入账户名"></el-input>
      </el-form-item>

      <el-form-item name="password">
        <el-input v-model="formData.password" type="password" clearable placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width: 100%;" @click="submitForm">登录</el-button>
      </el-form-item>
    </el-form>
    <Bubble v-for="(item, index) in bubbleList" v-bind="item" :key="index"></Bubble>
  </div>
</template>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  /* background: url('../../assets/bg.jpg') no-repeat; */
  background-image: radial-gradient(farthest-side at 0 0, rgba(255, 255, 255, 0.5), rgba(25, 146, 177, 0.5));
  background-size: cover;
}

.login-form {
  padding: 50px 30px;
  border: 1px solid #ccc;
  border-radius: 8px;
  width: 360px;
  background: var(--el-bg-color);
  z-index: 9;
}

.admin-name {
  flex: 1;
  text-align: center;
  font-size: 20px;
  color: var(--td-text-color-primary);
}
</style>
